package guiClasses;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import objectClasses.Category;
import objectClasses.Database;
import objectClasses.Draft;
import objectClasses.Entry;
import objectClasses.User;

public class EntryPanel extends GenericPanel{

	private JTextArea my_description;
	private JComboBox my_categories_JCB;
	private Database d;

	/**
	 * @wbp.parser.constructor 
	 * @param the_o_er
	 */
	public EntryPanel(final Observer the_o_er) {
		super(the_o_er);
		buildEntryPanel();
		d = Database.getInstance();
		JButton buildDraftButton = new JButton("Build Draft");
		buildDraftButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				setChanged();
				notifyObservers(new DraftPanel(my_o_ver).getPanel());

			}

		});


		my_panel.add(buildDraftButton, GBC.GBCGen(0, 0, 0, 3
				));
	}
	public EntryPanel(final Observer the_o_er, final Draft the_draft) {
		super(the_o_er);
		buildEntryPanel();
		my_panel.setPreferredSize(new Dimension(600,600));

		JButton submitButton = new JButton("Submit");
		submitButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
			User.changeUserType('C', GuiMain.getUser().getUserName());
				} catch (Exception ex) {
					
				}
				try{
			Database.addEntry(new Entry(GuiMain.getUser().getUserName(), my_description.getText(), 
			(String) my_categories_JCB.getSelectedItem(), the_draft));
				}
				catch(IllegalArgumentException ex){
					JOptionPane.showMessageDialog(my_panel,
						    "You already are registered for "+ my_categories_JCB.getSelectedItem()+ "!");
				}
			Database.saveFile();
			setChanged();
			notifyObservers(new AttendeePanel(my_o_ver).getPanel());
			}
		});
		
		JLabel imageLabel = new JLabel();

		BufferedImage iconImage = DraftPanel.createImage(the_o_er, the_draft);
		ImageIcon icon = new ImageIcon(iconImage.getScaledInstance(200, 200, 0));
		imageLabel.setIcon(icon);
		imageLabel.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.black));

		my_panel.add(imageLabel,GBC.GBCGen(0, 0, 0, 3));

		my_panel.add(submitButton, GBC.GBCGen(0, 0, 0, 4));
	}

	private void buildEntryPanel() {
		// old code for using the Category class to get the list of categories
//		my_categories = new JComboBox();
//		ArrayList<Category> listOfCats = Category.getCategories();
//		for(Category c: listOfCats) {
//			my_categories.addItem(c.getName());
//		}
		//new code to get the list of categories from the Database class.
		
		my_categories_JCB = new JComboBox();
		
		String[] categories = Database.getCategoryStrings();
		for(String c: categories) {
			my_categories_JCB.addItem(c);
		}
		
		my_panel.add(my_categories_JCB, GBC.GBCGen(0, 0, 0, 1));
		my_description = new JTextArea("Enter the description of the entry here.", 20, 5);
		my_panel.add(my_description, GBC.GBCGen(0,0,0,2));


	}


}
